﻿
Imports std
Imports storoomc
Imports storcore

Public Class cacheslices
    Implements storageInterface
    Private c As cacheManager(Of String, bytes) = Nothing
    Private maxKeyCount As UInt64

    Public Sub start()
        maxKeyCount = config.parameter(configNaming.memslices.name, _
                                       configNaming.memslices.keyCount, _
                                       configNaming.memslices.defaultValue.keyCount)
        c = New cacheManager(Of String, bytes)(False, maxKeyCount, _
                                               cacheManager(Of String, bytes).notRetireTicks)
    End Sub

    Public Function capacity(ByRef cap As UInt64) As Boolean Implements storageInterface.capacity
        cap = maxKeyCount - c.size()
        Return True
    End Function

    Public Function close() As Boolean Implements storageInterface.close
        Return True
    End Function

    Public Function delete(ByVal key As String) As Boolean Implements storageInterface.delete
        Return c.erase(key)
    End Function

    Public Function empty() As Boolean Implements storageInterface.empty
        Return c.empty()
    End Function

    Public Function full() As Boolean Implements storageInterface.full
        Return False
    End Function

    Public Function health() As Boolean Implements storageInterface.health
        Return Not c Is Nothing
    End Function

    Public Function keycount(ByRef count As UInt64) As Boolean Implements storageInterface.keycount
        count = c.size()
        Return True
    End Function

    Public Function list(ByRef keys As vector(Of String)) As Boolean Implements storageInterface.list
        If keys Is Nothing Then
            keys = New vector(Of String)()
        Else
            keys.clear()
        End If
        Dim ks As vector(Of String) = Nothing
        ks = keys
        Return c.traversal(Sub(ByRef s As String, ByRef b As bytes)
                               ks.push_back(s)
                           End Sub)
    End Function

    Public Function modify(ByVal key As String, ByVal buff As bytes) As Boolean Implements storageInterface.modify
        c.add(key, buff)
        Return True
    End Function

    Public Function open() As Boolean Implements storageInterface.open
        Return True
    End Function

    Public Function read(ByVal key As String, ByRef buff As bytes) As Boolean Implements storageInterface.read
        Return c.get(key, buff)
    End Function

    Public Function retire() As Boolean Implements storageInterface.retire
        c.clear()
        Return True
    End Function

    Public Function seek(ByVal key As String) As Boolean Implements storageInterface.seek
        Return c.have(key)
    End Function

    Public Function sizeof(ByVal key As String, ByRef size As UInt64) As Boolean Implements storageInterface.sizeof
        Dim b As bytes = Nothing
        If c.get(key, b) Then
            size = b.size()
            Return True
        Else
            Return False
        End If
    End Function

    Public Function status(ByRef allSlicesCount As UInt64, _
                           ByRef unhealthSlicesCount As UInt64) As Boolean Implements storageInterface.status
        allSlicesCount = 1
        unhealthSlicesCount = 0
        Return True
    End Function

    Public Function sync(ByVal desthost As String, ByVal destport As Int32) As Boolean Implements storageInterface.sync
        Return synchelper(Me, desthost, destport)
    End Function

    Public Function uniqueWrite(ByVal key As String, ByVal buff As bytes) As Boolean _
        Implements storageInterface.uniqueWrite
        Return Not seek(key) AndAlso write(key, buff)
    End Function

    Public Function valuesize(ByRef size As UInt64) As Boolean Implements storageInterface.valuesize
        size = c.size()
        Return True
    End Function

    Public Function write(ByVal key As String, ByVal buff As bytes) As Boolean Implements storageInterface.write
        c.add(key, buff)
        Return True
    End Function
End Class
